using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public enum EnemyState
{
    idle,
    run,
    attack
}
public class NewBehaviourScript : MonoBehaviour
{
    public EnemyState CurrentState = EnemyState.idle;
    private Animation ani;
    private Transform player;
    private NavMeshAgent agent;
    // Start is called before the first frame update
    void Start()
    {
        ani = GetComponent<Animation>();
        agent = GetComponent<NavMeshAgent>();
        player = GameObject.FindWithTag("Player").transform;
    }

    // Update is called once per frame

    float exitTime = 0;

    void Update()
    {
        


        float distance = Vector3.Distance(player.position, transform.position);

        EnemyState oldState = CurrentState;

        if (distance > 3 && distance <= 30)
        {
            CurrentState = EnemyState.run;
            agent.isStopped = false;
            agent.SetDestination(player.position);
        }
        else if (distance > 30)
        {
            CurrentState = EnemyState.idle;
            agent.isStopped = true;
        }
        else if (distance <= 3)
        {
            //exitTime = 0;
            CurrentState = EnemyState.attack;
        }

        if (oldState != CurrentState)
        {
            switch (CurrentState)
            {
                case EnemyState.idle:

                    ani.Play("Centaur_rig_idle1");

                    break;
                case EnemyState.run:

                    ani.Play("Centaur_rig_run");
                    break;
                case EnemyState.attack:
                    ani.Play("Centaur_rig_attack3");
                   
                    break;
            }
        }

        if(CurrentState == EnemyState.attack)
        {
            exitTime += Time.deltaTime;

            if (exitTime > 3.0f)
            {
                UnityEditor.EditorApplication.isPlaying = false;
            }
        }

    }
}
